package l1j.server.server.datatables;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.server.L1DatabaseFactory;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.utils.SQLUtil;

public class DailyTable {
	
	private static Logger _log = Logger.getLogger(DailyTable.class.getName());
	
	private static DailyTable _instance;
	
	public static DailyTable getInstance() {
		if (_instance == null) {
			_instance = new DailyTable();
		}
		return _instance;
	}
	
	private int _DailyID;
	private int _DailyStep;
	private int _PDaily;
	private int _Char_ID;
	private int _MQStep;
	private int _DailyOver = 0;
	private String _DailyEnd;
	
	public int getMQStep() {
		return _MQStep;
	}
	
	public void setMQtStep(int i) {
		_MQStep = i;
	}
	
	public int getCharID() {
		return _Char_ID;
	}
	
	public void setCharID(int i) {
		_Char_ID = i;
	}
	public int getDailyID() {
		return _DailyID;
	}
	
	public void setDailyID(int i) {
		_DailyID = i;
	}
	
	public int getDailyStep() {
		return _DailyStep;
	}

	public void setDailyStep(int i) {
		_DailyStep = i;
	}
	
	public int getPDaily() {
		return _PDaily;
	}

	public void setPDaily(int i) {
		_PDaily = i;
	}
	
	public int getDailyOver() {
		return _DailyOver;
	}
	
	public void setDailyOver(int i) {
		_DailyOver = i;
	}
	
	public String getDailyEnd() {
		return _DailyEnd;
	}
	
	public void setDailyEnd(String i) {
		_DailyEnd = i;
	}
	
	public void storeDaily(int char_id, int MQStep, int DailyID, int DailyStep, int PDaily) {
		
		Connection con = null;	
		PreparedStatement pstm = null;
		try {
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con
					.prepareStatement("INSERT INTO daily SET char_id=?, mqstep=?, dailyid=?, dailystep=?, pdaily=?");
			pstm.setInt(1, char_id);
			pstm.setInt(2, MQStep);
			pstm.setInt(3, DailyID);
			pstm.setInt(4, DailyStep);
			pstm.setInt(5, PDaily);
			pstm.execute();
		} catch (Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);

		} finally {
			SQLUtil.close(pstm);
			SQLUtil.close(con);

		}
	}
	
	public void updateDaily(int char_id, int MQStep, int DailyID, int DailyStep, int PDaily) {
		Connection con = null;
		PreparedStatement pstm = null;
		try {
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con
					.prepareStatement("UPDATE "
							+ "daily SET mqstep=?, dailyid=? ,dailystep=?, pdaily=? WHERE char_id=?");
			pstm.setInt(1, MQStep);
			pstm.setInt(2, DailyID);
			pstm.setInt(3, DailyStep);
			pstm.setInt(4, PDaily);		
			pstm.setInt(5, char_id);
			pstm.execute();
		} catch (Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
	}
	
	public void updateDailyStatus(L1PcInstance pc, int char_id, int DailyStep) {
		Connection con = null;
		PreparedStatement pstm = null;
		DailyTable dq = this.loadDaily(pc);
		String TempDailyEnd = dq.getDailyEnd();		
		this.setDailyEnd(TempDailyEnd.replaceFirst(String.valueOf(TempDailyEnd.charAt(dq.getDailyStep()-1)), "1"));
		System.out.println("CCC:"+this.getDailyEnd());
		try {
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con
					.prepareStatement("UPDATE daily SET dailyid=0, dailystep=0, dailyend=? WHERE char_id=?");
			//pstm.setInt(1, DailyStep);	
			pstm.setString(1, this._DailyEnd);
			pstm.setInt(2, char_id);
			pstm.execute();
		} catch (Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
	}	
	
	public int countDaily(int char_id) {
		int result = 0;
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con
					.prepareStatement("SELECT count(*) as cnt FROM daily WHERE char_id=?");
			pstm.setInt(1, char_id);
			rs = pstm.executeQuery();
			if (rs.next()) {
				result = rs.getInt("cnt");
			}
		} catch (SQLException e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(rs);
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
		return result;
	}
	
	public DailyTable loadDaily(L1PcInstance pc) {
		DailyTable DT = null;
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("SELECT * FROM daily WHERE char_id=?");
			pstm.setInt(1, pc.getId());
			rs = pstm.executeQuery();
			while (rs.next()) {
				DT = new DailyTable();
				DT.setMQtStep(rs.getInt("mqstep"));
				DT.setDailyID(rs.getInt("dailyid"));
				DT.setDailyStep(rs.getInt("dailystep"));
				DT.setPDaily(rs.getInt("pdaily"));
				DT.setDailyEnd(rs.getString("dailyend"));
			}
		}
		catch (SQLException e) { 
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		}
		finally {
			SQLUtil.close(rs, pstm, con);
		}
		return DT;
	}

}
